package machine.api;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import machine.bean.Setu;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * 涩图api
 *
 * @author zhanghui145
 * @date 2021/12/25 13:47
 * @since 1.0.0
 */
@Service
public class SetuApi {

    private static final String URI = "https://api.lolicon.app/setu/v2";

    public Setu getPicture(){
        HttpResponse response;
        StringBuilder stringBuilder = new StringBuilder(URI);
        stringBuilder.append("?r18=2&num=1");
        try {
            response = HttpRequest.get(stringBuilder.toString()).execute();
        }catch (Exception e){
            throw new RuntimeException("连接超时~");
        }
        String rs = response.body();
        JSONArray array = JSONUtil.parseArray(JSONUtil.parseObj(rs).get("data"));
        Assert.isTrue(!array.isEmpty(), "未找到涩图~");
        return JSONUtil.parse(array.get(0)).toBean(Setu.class);
    }
}